import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2025-01-21
 * Time: 11:23
 */
public class SummaryRanges {
    public List<String> summaryRanges(int[] nums) {
        List<String> ret = new ArrayList<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int start = nums[i];
            while (i < n - 1 && nums[i] + 1 == nums[i+1]) {
                i++;
            }
            if (i == n - 2 && nums[i] + 1 == nums[i + 1]) {
                i = i + 1;
            }
            int end = nums[i];
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(start);
            if (start != end) {
                stringBuilder.append("->");
                stringBuilder.append(end);
            }
            ret.add(stringBuilder.toString());
        }
        return ret;
    }

    public static void main(String[] args) {
        SummaryRanges summaryRanges = new SummaryRanges();
        summaryRanges.summaryRanges(new int[]{0,2,3,4,6,8,9});
    }
}
